﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Icoders.MarketTracker.Core.MasterData;
using Icoders.MarketTracker.Web.Lib.MasterDataDTO;
using Icoders.MarketTracker.Web.Lib.Response.Containers;

namespace Icoders.MarketTracker.Web.Lib.Response.Services.Impl
{
    public class PullMasterDataResponseBuilder : IPullMasterDataResponseBuilder
    {
        private IClientMasterDataTrackerRepository _clientMasterDataTrackerRepository;
        private IMasterDataToDTOMapping _masterDataToDTOMapping;

        public PullMasterDataResponseBuilder(IClientMasterDataTrackerRepository clientMasterDataTrackerRepository, IMasterDataToDTOMapping masterDataToDtoMapping)
        {
            _clientMasterDataTrackerRepository = clientMasterDataTrackerRepository;
            _masterDataToDTOMapping = masterDataToDtoMapping;
        }

        public ResponseMasterDataInfo GetMasterDataInfo(int applicationId, string entityName)
        {
            SyncMasterDataCollective mdc = (SyncMasterDataCollective)Enum.Parse(typeof(SyncMasterDataCollective), entityName, true);

            MasterDataInfo mdi = new MasterDataInfo { EntityName = entityName };
            int entityId = (int)mdc;
            MasterDataEnvelope envelope = null;
            envelope = _clientMasterDataTrackerRepository.GetMasterDataEnvelope(applicationId, entityId);
            List<BaseDTO> masterBaseDTOList = new List<BaseDTO>();
            if (envelope == null)
            {
                envelope = new MasterDataEnvelope()
                {
                    masterData = new List<MasterEntityBase>(),
                    masterDataName = ""
                };
            }

            foreach (MasterEntityBase entity in envelope.masterData)
            {
                BaseDTO masterBaseDTO = MapResolver(mdc, entity);
                if (masterBaseDTO == null)
                    continue;
                masterBaseDTOList.Add(masterBaseDTO);

            }

            mdi.MasterDataItems = masterBaseDTOList.ToArray();

            return new ResponseMasterDataInfo()
            {
                ErrorInfo = "Success",
                MasterData = mdi
            };
        }

        private BaseDTO MapResolver(SyncMasterDataCollective mdc, MasterEntityBase me)
        {
            switch (mdc)
            {
                case SyncMasterDataCollective.Country:
                    return _masterDataToDTOMapping.Map(me as Country);
                case SyncMasterDataCollective.City:
                    return _masterDataToDTOMapping.Map(me as City);
                case SyncMasterDataCollective.Area:
                    return _masterDataToDTOMapping.Map(me as Area);
                case SyncMasterDataCollective.Zone:
                    return _masterDataToDTOMapping.Map(me as Zone);
                case SyncMasterDataCollective.PointOfPurchase:
                    return _masterDataToDTOMapping.Map(me as PointOfPurchase);
                case SyncMasterDataCollective.PointOfPurchaseType:
                    return _masterDataToDTOMapping.Map(me as PointOfPurchaseType);
                case SyncMasterDataCollective.Product:
                    return _masterDataToDTOMapping.Map(me as Product);
                case SyncMasterDataCollective.Reason:
                    return _masterDataToDTOMapping.Map(me as Reason);
                case SyncMasterDataCollective.User:
                    return _masterDataToDTOMapping.Map(me as User);
                case SyncMasterDataCollective.Client:
                    return _masterDataToDTOMapping.Map(me as Client);
                default:
                    throw new Exception("Failed to map to DTO " + mdc.ToString());
            }
        }
    }
}
